package com.module.ble.hDu

import android.os.RemoteException
import android.util.Log
import com.common.base.app.extras.doOnIOThread
import com.common.base.utils.JsonUtils
import com.common.base.utils.LogUtils
import com.module.ble.utils.AizoSDKUtil
import com.module.ble.utils.BleOtherUtil
import com.sxr.sdk.ble.keepfit.aidl.IRemoteService

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/3/25
 *
 * 描述：华嘟命令发送管理
 *
 * 修订历史：
 *
 */
object HDuSendCommandManage {

    private const val TAG = "HDuSendCommandManage"

    fun callRemoteIsConnected(mService: IRemoteService?): Boolean {
        var isConnected = false
        if (mService != null) {
            try {
                isConnected = mService.isConnectBt
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return isConnected
    }

    fun callRemoteGetConnectedDevice(mService: IRemoteService?): String {
        var deviceMac = ""
        if (mService != null) {
            try {
                deviceMac = mService.connectedDevice
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return deviceMac
    }


    fun callRemoteDisconnect(mService: IRemoteService?): Boolean {
        if (mService != null) {
            try {
                mService.disconnectBt(true)
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return false
    }

    fun callRemoteIsAuthorize(mService: IRemoteService?): Int {
        var isAuthorize = 0
        if (mService != null) {
            try {
                isAuthorize = mService.isAuthrize
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return isAuthorize
    }

    /**
     * 华嘟设置设备时间
     */
    fun callSetDeviceTime(mService: IRemoteService?): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setDeviceTime()
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return false
    }

    /**
     * 华嘟AppId信息
     */
    fun callSetAppId(mService: IRemoteService?, appId: String): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setAppId(appId)
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return false
    }

    /**
     * 读取戒指信息
     */
    fun callRemoteGetDeviceInfo(mService: IRemoteService?): Boolean {
        if (mService != null) {
            try {
                mService.deviceInfo
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return true
    }

    /**
     * 读取戒指电量
     */
    fun callRemoteGetDeviceBattery(mService: IRemoteService?): Boolean {
        if (mService != null) {
            try {
                AizoSDKUtil.mHDuService?.deviceBatery
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return true
    }

    /**
     * 华嘟自动测试心率
     */
    fun callRemoteSetAutoHeartMode(mService: IRemoteService?, enable: Boolean) {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setAutoHeartMode(enable, 0, 0, 23, 59, 15, 2) // 00:00 - 23:00  15min 2min
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
    }

    /**
     * 自动测试心率
     */
    fun callRemoteSetAutoHeartMode(mService: IRemoteService?, enable: Boolean, monitorTime: Int = 15): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setAutoHeartMode(enable, 0, 0, 23, 59, monitorTime, 2) // 00:00 - 23:00  15min 2min
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return false
    }

    /**
     * 查询某天详细运动信息
     *
     * @param type 1： 取得当天详细运动信息, 返回步数与睡眠  2：取得当天详细心率数据,  12: 取得当天详细体温数据
     * @param type 返回数据 type : 1 运动信息  2 睡眠信息 3 心率信息 12 体温数据;
     * 当type = 1 时  step为步数 心率为0（无效）;
     * 当type = 2 时  step为睡眠质量数据;如果为0表明没有记录或没有睡眠，有记录时的取值范围为（1-100）。100为最好的睡眠质量，1为最差的睡眠质量） 心率为0（无效）
     * 当type = 3 时  step为0（无效） heartrate为心率值
     * 当type = 12 时 step为体表温度（>300, 无效） heartrate为体温温度（>300, 无效）
     * @param day: 时间范围 0-7 （0：当天，1：前一天）
     * 回调 onGetDataByDay
     */
    fun callRemoteGetData(mService: IRemoteService?, type: Int, day: Int,): Boolean {
        val result: Int
        if (mService != null) {
            doOnIOThread { LogUtils.e(TAG, "HDU: ${JsonUtils.toJson(BleOtherUtil.allSleepInfo)}") }
            // Set去重
            val uniqueSleepInfo = BleOtherUtil.allSleepInfo.toSet().toMutableList()
            uniqueSleepInfo.sortedBy { it.createdTime }
            doOnIOThread { LogUtils.e(TAG, "HDU2: ${JsonUtils.toJson(BleOtherUtil.allSleepInfo)}") }
            BleOtherUtil.allHeartRates.clear()
            BleOtherUtil.allSleepInfo.clear()
            try {
                result = mService.getDataByDay(type, day)
                Log.e(TAG, "HDU---getDataByDay===$result")
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "HDU---getDataByDay---Remote call error!")
            }
        } else {
            Log.e(TAG, "HDU---getDataByDay---Service is not available yet!")
        }
        return false
    }


    /**
     * @param enable 启动\关闭实时心率测量
     * 开启运动心率，第二个参数填0，第三个参数1-5，分别表示步行，跑步，骑行，爬山，游泳
     */
    fun callRemoteSetHeartRateMode(mService: IRemoteService?, enable: Boolean, seconds: Int = 60, mode: Int = 0): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setHeartRateMode(enable, seconds, mode)
                Log.e(TAG, "HDu-sdk---callRemoteSetHeartRateMode---enable:===$enable---result:$result")
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "HDu-sdk---callRemoteSetHeartRateMode Remote call error!")
            }
        } else {
            Log.e(TAG, "HDu-sdk---callRemoteSetHeartRateMode Service is not available yet!")
        }
        return false
    }

    /**
     * 打开关闭血压，血氧，疲劳度
     */
    fun callRemoteOpenBlood(mService: IRemoteService?, enable: Boolean): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.setBloodPressureMode(enable)
                Log.e(TAG, "HDu-sdk---callRemoteOpenBlood---enable:===$enable---result:$result")
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "HDu-sdk---callRemoteOpenBlood---Remote call error!")
            }
        } else {
            Log.e(TAG, "HDu-sdk---callRemoteOpenBlood---Service is not available yet!")
        }
        return false
    }


    /**
     * 获取当前运动数据
     */
    fun callGetCurSportData(mService: IRemoteService?): Boolean {
        val result: Int
        if (mService != null) {
            try {
                result = mService.curSportData
                return true
            } catch (e: RemoteException) {
                e.printStackTrace()
                Log.e(TAG, "Remote call error!")
            }
        } else {
            Log.e(TAG, "Service is not available yet!")
        }
        return false
    }

    fun openHeartAndBloodMode(mOpen:Boolean = true, mode: Int = 0){
        callRemoteSetHeartRateMode(AizoSDKUtil.mHDuService, mOpen, mode)
        callRemoteOpenBlood(AizoSDKUtil.mHDuService, mOpen)
    }
}